$(document).ready(function() {
    $("#borrow-table-modify").hide();
    $(document).on('click','.button-container-borrow', function (e) {
    console.log("我点击了修改按钮哦!")
        var id = $(this).attr('id')
        console.log(id)
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: '/lent/modify',
            data: {"id":id},
            dataType: "json",
            success: function (data) {
                    let $row = $('<tr>');
                    let $zero = $('<td>'),
                        $first = $('<td>'),
                        $second = $('<td>'),
                        $third = $('<td>'),
                        $forth = $('<td>');


                    $zero.text(data[0].bid).appendTo($row);
                    $zero.addClass("remake");
                    $first.text(data[0].username).appendTo($row);
                    $first.addClass("remake");
                    $second.text(data[0].borrowingTime).appendTo($row);
                    $second.addClass("remake");
                    $third.text(data[0].remainTime).appendTo($row);
                    $third.addClass("remake");
                    $forth.text(data[0].score).appendTo($row);
                    $forth.addClass("remake");

                    $row.appendTo('#modifyTable-borrow tbody');


                    var remake = document.getElementsByClassName("remake");

                    for (let i = 0; i < remake.length; i++) {

                        remake[i].onclick = () => {
                            remake[i].style.display = "none";
                            var para = document.createElement("input");
                            var ctd = document.createElement("td");
                            para.value = remake[i].innerHTML;
                            remake[i].parentNode.insertBefore(ctd, remake[i]);
                            ctd.appendChild(para, ctd);
                            para.focus();
                            para.onblur = () => {
                                remake[i].innerHTML = para.value;
                                remake[i].parentNode.removeChild(ctd);
                                remake[i].style.display = ""
                            }
                        }
                    }


            }
        });
        $("#borrow-table-modify").show();
    });
    $(document).on('click','.button-delete-borrow', function (e) {
        var name = $(this).attr('name')
        console.log(name)
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: '/lent/delete',
            data: {"name":name},
            dataType: "json",
            success: function (data) {
            $('tbody tr').remove();
//                var jsonObj = JSON.parse(data);
                $.each(data, function (index, item) {
                             console.log(item.bid);
                    let $row = $('<tr>');
                    $('<td>').text(item.bid).appendTo($row);
                    $('<td>').text(item.username).appendTo($row);
                    $('<td>').text(item.borrowingTime).appendTo($row);
                    $('<td>').text(item.remainTime).appendTo($row);
                    $('<td>').text(item.score).appendTo($row);

                    let $cell = $('<td>');
                    let $btn_modify = $('<button>'),
                        $btn_delete = $('<button>'),
                        $btn_scan = $('<button>');

                    $btn_modify.addClass("button-container-borrow");
                    $btn_modify.attr('id',item.bid)

                    $btn_delete.addClass("button-delete-borrow");
                    $btn_delete.attr('name',item.bid)

                    $btn_scan.addClass("button-return-borrow");
                    $btn_scan.attr('name',item.bid)

                    $btn_modify.text('编辑').appendTo($cell);
                    $btn_delete.text('删除').appendTo($cell);
                    $btn_scan.text('扫码归还').appendTo($cell);

                    $cell.appendTo($row);

                    $row.appendTo('#borrow-table tbody');
                    });
            }
        });
        $("#borrow-table-modify").hide();
    });
    $(document).on('click','.cancel-button-borrowers', function (e) {
        console.log("我点击了返回按钮！")
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: '/lent/returnBorrow',
            dataType: "json",
            success: function (data) {
            $('tbody tr').remove();
//                var jsonObj = JSON.parse(data);
                $.each(data, function (index, item) {
                    console.log(item.bid);
                    let $row = $('<tr>');
                    $('<td>').text(item.bid).appendTo($row);
                    $('<td>').text(item.username).appendTo($row);
                    $('<td>').text(item.borrowingTime).appendTo($row);
                    $('<td>').text(item.remainTime).appendTo($row);
                    $('<td>').text(item.score).appendTo($row);

                    let $cell = $('<td>');
                    let $btn_modify = $('<button>'),
                        $btn_delete = $('<button>'),
                        $btn_scan = $('<button>');

                    $btn_modify.addClass("button-container-borrow");
                    $btn_modify.attr('id',item.bid)

                    $btn_delete.addClass("button-delete-borrow");
                    $btn_delete.attr('name',item.bid)

                    $btn_scan.addClass("button-return-borrow");
                    $btn_scan.attr('name',item.bid)

                    $btn_modify.text('编辑').appendTo($cell);
                    $btn_delete.text('删除').appendTo($cell);
                    $btn_scan.text('扫码归还').appendTo($cell);

                    $cell.appendTo($row);

                    $row.appendTo('#borrow-table tbody');
                    });
            }
        });
        $("#borrow-table-modify").hide();
    });
    $(".confirm-button-borrowers").click(function (e) {
        e.preventDefault();
        $row = document.getElementById("modifyTable-borrow");
        var data = [];
        for (var i = 0, rows = $row.rows.length; i < rows; i++) {
            for (var j = 0, cells = $row.rows[i].cells.length; j < cells; j++) {
                if (!data[i]) {
                    data[i] = [];
                }
                data[i][j] = $row.rows[i].cells[j].innerHTML;
            }
        }
        $.ajax({
            type: 'PUT',
            url: '/lent/modifyResult',
            data:{
                "bid":data[1][0],
                "username":data[1][1],
                "borrowingTime":data[1][2],
                "remainTime":data[1][3],
                "score":data[1][4],
            },
            dataType:"json",
            success: function (data) {
                $('tbody tr').remove();
                $("#borrow-table-modify").hide();
                var jsonObj = JSON.parse(data);
                $.each(jsonObj, function (index, item) {
                    console.log(item.bid);
                    let $row = $('<tr>');
                    $('<td>').text(item.bid).appendTo($row);
                    $('<td>').text(item.username).appendTo($row);
                    $('<td>').text(item.borrowingTime).appendTo($row);
                    $('<td>').text(item.remainTime).appendTo($row);
                    $('<td>').text(item.score).appendTo($row);


                    let $cell = $('<td>');
                    let $btn_modify = $('<button>'),
                        $btn_delete = $('<button>');

                    $btn_modify.addClass("button-container-borrow");
                    $btn_modify.attr('id',item.bid)


                    $btn_delete.addClass("button-delete-borrow");
                    $btn_delete.attr('name',item.bid)
                    $btn_modify.text('编辑').appendTo($cell);
                    $btn_delete.text('删除').appendTo($cell);
                    $cell.appendTo($row);

                    $row.appendTo('#borrow-table tbody');
                    });
                }
            })
        });
    $(document).on('click','.button-return-borrow', function (e) {
        console.log("我点击了扫码归还按钮！")
        e.preventDefault();
        var name = $(this).attr('name');
        console.log("name = "+name);
        $.ajax({
            type: 'post',
            url: '/lent/returnByQRcode',
            dataType: "json",
            data: {"name":name},
            success: function (data) {

                $('tbody tr').remove();
                $.each(data, function (index, item) {
                    console.log(item.bid);
                    let $row = $('<tr>');
                    $('<td>').text(item.bid).appendTo($row);
                    $('<td>').text(item.username).appendTo($row);
                    $('<td>').text(item.borrowingTime).appendTo($row);
                    $('<td>').text(item.remainTime).appendTo($row);
                    $('<td>').text(item.score).appendTo($row);

                    let $cell = $('<td>');
                    let $btn_modify = $('<button>'),
                        $btn_delete = $('<button>'),
                        $btn_scan = $('<button>');

                    $btn_modify.addClass("button-container-borrow");
                    $btn_modify.attr('id',item.bid)

                    $btn_delete.addClass("button-delete-borrow");
                    $btn_delete.attr('name',item.bid)

                    $btn_scan.addClass("button-return-borrow");
                    $btn_scan.attr('name',item.bid)

                    $btn_modify.text('编辑').appendTo($cell);
                    $btn_delete.text('删除').appendTo($cell);
                    $btn_scan.text('扫码归还').appendTo($cell);

                    $cell.appendTo($row);

                    $row.appendTo('#borrow-table tbody');
                    });
        }});
        $("#borrow-table-modify").hide();
    });
});
